package com.example.danmuapplication1111;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.ui.widget.DanmakuView;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = "Main2Activity";
    ImageView imageView;
Handler handler=new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        super.handleMessage(msg);
       Bitmap bitmap= (Bitmap) msg.obj;//取对象
        //用对象
       imageView.setImageBitmap(bitmap);
    }
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        imageView=findViewById(R.id.iv_ing_tuantuan);

        new  Thread(new Runnable() {
            @Override
            public void run() {
                getNetImg();//进入子线程
            }
        }).start();
    }

    private void getNetImg() {
        try {
            URL url=new URL("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3654506504,2577427162&fm=26&gp=0.jpg");//放链接
            HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();//打开链接
            urlConnection.setRequestMethod("GET");
          //  Log.d(TAG, "getNetImg: "+urlConnection.getResponseCode());
            if(urlConnection.getResponseCode()==200){
                InputStream inputStream=urlConnection.getInputStream();//读的流   获取网络    一个一个流过去的
                // OutputStream outputStream=urlConnection.getOutputStream();//写的流
                //转码
                Bitmap bitmap= BitmapFactory.decodeStream(inputStream);//获取图片
               // imageView.setImageBitmap(bitmap);//把图表绑定到iamgView

                Message message= handler.obtainMessage();//连接主线程
                message.obj=bitmap;//放到obj
                handler.sendMessage(message);//发信息给主线程
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
